
Partial Class Case_caseParticipantRepInfo
    Inherits System.Web.UI.UserControl
    Implements IPermission

    Public ReadOnly Property SectionName() As String Implements IPermission.SectionName
        Get
            Return "Representative"
        End Get
    End Property

    Private ReadOnly Property CurrentPermission() As Cdm.DataCore.UserSecurity.Permission.PermissionType
        Get
            If TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.LegalGuardian Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseGuardianPermissions
            ElseIf TypeOf (Me.PrimaryObject) Is Cdm.DataCore.Entities.ParticipantRepresentative Then
                Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseRepPermissions
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Property PrimaryObject() As Cdm.DataCore.ConfigurableObject Implements IPermission.PrimaryObject
        Get
            Dim pg As IPermission
            Dim pt As Cdm.DataCore.Entities.Participant
            pg = Me.Page
            pt = pg.PrimaryObject
            Return pt.Representative
        End Get
        Set(ByVal value As Cdm.DataCore.ConfigurableObject)
            SetPageControls()
        End Set
    End Property

    Public ReadOnly Property IsParticipantRepChecked() As Boolean
        Get
            Return Me.HasRepCheckBox.Checked
        End Get
    End Property

    Public ReadOnly Property ControlCollection() As System.Web.UI.ControlCollection Implements IPermission.ControlCollection
        Get
            Return Me.Controls
        End Get
    End Property

    Public ReadOnly Property EntityPermissionType() As Cdm.DataCore.UserSecurity.Permission.PermissionType Implements IPermission.EntityPermissionType
        Get
            Return Cdm.DataCore.UserSecurity.Permission.PermissionType.CaseParticipantRep
        End Get
    End Property

    Public Sub SaveData()
        EntityController.SetObjectPropertiesFromPage(Me)
        Me.PrimaryObject.SpecialFieldsValues = Me.SpecialFieldControl.SaveSpecialFields()

        Me.RepGuardianPermissions.SetPermissions()
    End Sub

    Private Sub SetPageControls()
        ResetControls()

        EntityController.SetPagePropertiesFromObject(Me, True)
        Dim pg As IPermission = Me.Page
        Dim pt As Cdm.DataCore.Entities.Participant = pg.PrimaryObject

      


        Me.RepPanel.Enabled = pt.HasRepresentative
        Me.HasRepCheckBox.Checked = pt.HasRepresentative
        If pt.ParticipantId > 0 Then
            Me.HasRepCheckBox.Enabled = UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        Else
            Me.HasRepCheckBox.Enabled = UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End If

        Me.SpecialFieldControl.Editable = HasRepCheckBox.Enabled
        Me.SpecialFieldControl.PrimaryObject = Me.PrimaryObject

    End Sub

    Public Sub ResetControls() Implements IPermission.ResetControls
        'Reset the labels, so that the Participant Rep text is only replaced once.
        Me.TitleLabel.Text = "Participant Rep"
        Me.HasRepLabel.Text = "Has Participant Rep?"
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        If Me.IsPostBack Then
            Me.SpecialFieldControl.Editable = CheckEditable()
            Me.SpecialFieldControl.PrimaryObject = Me.PrimaryObject
        End If

    End Sub

    Private Function CheckEditable() As Boolean
        If Me.PrimaryObject.Id > 0 Then
            Return UserAuthorization.HasUpdatePermission(Me.EntityPermissionType)
        Else
            Return UserAuthorization.HasInsertPermission(Me.EntityPermissionType)
        End If

    End Function

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        If Me.IsPostBack Then
            ResetControls()
            EntityController.SetPagePropertiesFromObject(Me, False)
        Else
            Dim participantRepTitle As String = EntityController.GetSingleDisplayName("Participant Rep")
            Me.TitleLabel.Text = participantRepTitle
            Me.HasRepLabel.Text = "Has " & participantRepTitle & "?"
        End If

        If UserAuthorization.HasViewPermission(Me.CurrentPermission) Then
            Me.PermissionsButton.Visible = True
        Else
            Me.PermissionsButton.Visible = False
        End If

    End Sub

    Protected Sub hasRepresentative_CheckedChanged(ByVal sender As Object, _
                                                   ByVal e As System.EventArgs) _
                                                   Handles HasRepCheckBox.CheckedChanged

        Me.RepPanel.Enabled = HasRepCheckBox.Checked

    End Sub

    Protected Sub PermissionsButton_Click(ByVal sender As Object, _
                                          ByVal e As System.EventArgs) _
                                          Handles PermissionsButton.Click

        If Me.PermissionsButton.Text = "Show Permissions" Then

            With Me.RepGuardianPermissions
                .Visible = True
                .BindGridView()
                .SetPermissions()
                .CheckRolePermissions()
            End With
            Me.PermissionsButton.Text = "Hide Permissions"
            Me.WholePagePanel.Visible = False

        Else

            Me.RepGuardianPermissions.Visible = False
            Me.PermissionsButton.Text = "Show Permissions"
            Me.WholePagePanel.Visible = True
            
        End If

    End Sub

End Class
